2 use MediaWiki\MediaWikiServices
;
3 use MediaWiki\Revision\MutableRevisionRecord
;
4 use MediaWiki\Revision\RevisionStore
;
5 use MediaWiki\Revision\IncompleteRevisionException
;
6 use MediaWiki\Revision\RevisionRecord
;
7 use MediaWiki\Revision\SlotRecord
;
10 * RevisionDbTestBase contains test cases for the Revision class that have Database interactions.
15 abstract class RevisionDbTestBase
extends MediaWikiTestCase
{
18 * @var WikiPage $testPage
22 public function __construct( $name = null, array $data = [], $dataName = '' ) {
23 parent
::__construct( $name, $data, $dataName );
25 $this->tablesUsed
= array_merge( $this->tablesUsed
,
48 protected function addCoreDBData() {
49 // Blank out. This would fail with a modified schema, and we don't need it.
55 abstract protected function getMcrMigrationStage();
60 abstract protected function getMcrTablesToReset();
62 protected function setUp() {
63 $this->tablesUsed +
= $this->getMcrTablesToReset();
67 $this->mergeMwGlobalArrayValue(
71 12313 => 'Dummy_talk',
75 $this->mergeMwGlobalArrayValue(
76 'wgNamespaceContentModels',
78 12312 => DummyContentForTesting
::MODEL_ID
,
82 $this->mergeMwGlobalArrayValue(
85 DummyContentForTesting
::MODEL_ID
=> 'DummyContentHandlerForTesting',
86 RevisionTestModifyableContent
::MODEL_ID
=> 'RevisionTestModifyableContentHandler',
90 $this->setMwGlobals( [
91 'wgMultiContentRevisionSchemaMigrationStage' => $this->getMcrMigrationStage(),
92 'wgContentHandlerUseDB' => $this->getContentHandlerUseDB(),
93 'wgCommentTableSchemaMigrationStage' => MIGRATION_NEW
,
94 'wgActorTableSchemaMigrationStage' => SCHEMA_COMPAT_OLD
,
97 $this->overrideMwServices();
99 if ( !$this->testPage
) {
101 * We have to create a new page for each subclass as the page creation may result
102 * in different DB fields being filled based on configuration.
104 $this->testPage
= $this->createPage( __CLASS__
, __CLASS__
);
109 * @param string $model
112 protected function getMockTitle() {
113 $mock = $this->getMockBuilder( Title
::class )
114 ->disableOriginalConstructor()
116 $mock->expects( $this->any() )
117 ->method( 'getNamespace' )
118 ->will( $this->returnValue( $this->getDefaultWikitextNS() ) );
119 $mock->expects( $this->any() )
120 ->method( 'getPrefixedText' )
121 ->will( $this->returnValue( __CLASS__
) );
122 $mock->expects( $this->any() )
123 ->method( 'getDBkey' )
124 ->will( $this->returnValue( __CLASS__
) );
125 $mock->expects( $this->any() )
126 ->method( 'getArticleID' )
127 ->will( $this->returnValue( 23 ) );
132 abstract protected function getContentHandlerUseDB();
134 private function makeRevisionWithProps( $props = null ) {
135 if ( $props === null ) {
139 if ( !isset( $props['content'] ) && !isset( $props['text'] ) ) {
140 $props['text'] = 'Lorem Ipsum';
143 if ( !isset( $props['user_text'] ) ) {
144 $user = $this->getTestUser()->getUser();
145 $props['user_text'] = $user->getName();
146 $props['user'] = $user->getId();
149 if ( !isset( $props['user'] ) ) {
153 if ( !isset( $props['comment'] ) ) {
154 $props['comment'] = 'just a test';
157 if ( !isset( $props['page'] ) ) {
158 $props['page'] = $this->testPage
->getId();
161 if ( !isset( $props['content_model'] ) ) {
162 $props['content_model'] = CONTENT_MODEL_WIKITEXT
;
165 $rev = new Revision( $props );
167 $dbw = wfGetDB( DB_MASTER
);
168 $rev->insertOn( $dbw );
174 * @param string $titleString
175 * @param string $text
176 * @param string|null $model
180 private function createPage( $titleString, $text, $model = null ) {
181 if ( !preg_match( '/:/', $titleString ) &&
182 ( $model === null ||
$model === CONTENT_MODEL_WIKITEXT
)
184 $ns = $this->getDefaultWikitextNS();
185 $titleString = MWNamespace
::getCanonicalName( $ns ) . ':' . $titleString;
188 $title = Title
::newFromText( $titleString );
189 $wikipage = new WikiPage( $title );
191 // Delete the article if it already exists
192 if ( $wikipage->exists() ) {
193 $wikipage->doDeleteArticle( "done" );
196 $content = ContentHandler
::makeContent( $text, $title, $model );
197 $wikipage->doEditContent( $content, __METHOD__
, EDIT_NEW
);
202 private function assertRevEquals( Revision
$orig, Revision
$rev = null ) {
203 $this->assertNotNull( $rev, 'missing revision' );
205 $this->assertEquals( $orig->getId(), $rev->getId() );
206 $this->assertEquals( $orig->getPage(), $rev->getPage() );
207 $this->assertEquals( $orig->getTimestamp(), $rev->getTimestamp() );
208 $this->assertEquals( $orig->getUser(), $rev->getUser() );
209 $this->assertEquals( $orig->getContentModel(), $rev->getContentModel() );
210 $this->assertEquals( $orig->getContentFormat(), $rev->getContentFormat() );
211 $this->assertEquals( $orig->getSha1(), $rev->getSha1() );
215 * @covers Revision::getRecentChange
217 public function testGetRecentChange() {
218 $rev = $this->testPage
->getRevision();
219 $recentChange = $rev->getRecentChange();
221 // Make sure various attributes look right / the correct entry has been retrieved.
222 $this->assertEquals( $rev->getTimestamp(), $recentChange->getAttribute( 'rc_timestamp' ) );
224 $rev->getTitle()->getNamespace(),
225 $recentChange->getAttribute( 'rc_namespace' )
228 $rev->getTitle()->getDBkey(),
229 $recentChange->getAttribute( 'rc_title' )
231 $this->assertEquals( $rev->getUser(), $recentChange->getAttribute( 'rc_user' ) );
232 $this->assertEquals( $rev->getUserText(), $recentChange->getAttribute( 'rc_user_text' ) );
233 $this->assertEquals( $rev->getComment(), $recentChange->getAttribute( 'rc_comment' ) );
234 $this->assertEquals( $rev->getPage(), $recentChange->getAttribute( 'rc_cur_id' ) );
235 $this->assertEquals( $rev->getId(), $recentChange->getAttribute( 'rc_this_oldid' ) );
239 * @covers Revision::insertOn
241 public function testInsertOn_success() {
242 $parentId = $this->testPage
->getLatest();
244 // If an ExternalStore is set don't use it.
245 $this->setMwGlobals( 'wgDefaultExternalStore', false );
247 $rev = new Revision( [
248 'page' => $this->testPage
->getId(),
249 'title' => $this->testPage
->getTitle(),
250 'text' => 'Revision Text',
251 'comment' => 'Revision comment',
254 $revId = $rev->insertOn( wfGetDB( DB_MASTER
) );
256 $this->assertInternalType( 'integer', $revId );
257 $this->assertSame( $revId, $rev->getId() );
259 // getTextId() must be an int!
260 $this->assertInternalType( 'integer', $rev->getTextId() );
262 $mainSlot = $rev->getRevisionRecord()->getSlot( SlotRecord
::MAIN
, RevisionRecord
::RAW
);
264 // we currently only support storage in the text table
265 $textId = MediaWikiServices
::getInstance()
267 ->getTextIdFromAddress( $mainSlot->getAddress() );
271 [ 'old_id', 'old_text' ],
273 [ [ strval( $textId ), 'Revision Text' ] ]
286 "rev_id = {$rev->getId()}",
288 strval( $rev->getId() ),
289 strval( $this->testPage
->getId() ),
294 's0ngbdoxagreuf2vjtuxzwdz64n29xm',
299 public function provideInsertOn_exceptionOnIncomplete() {
300 $content = new TextContent( '' );
301 $user = User
::newFromName( 'Foo' );
303 yield
'no parent' => [
305 'content' => $content,
309 IncompleteRevisionException
::class,
310 "rev_page field must not be 0!"
313 yield
'no comment' => [
315 'content' => $content,
319 IncompleteRevisionException
::class,
320 "comment must not be NULL!"
323 yield
'no content' => [
329 IncompleteRevisionException
::class,
330 "Uninitialized field: content_address" // XXX: message may change
335 * @dataProvider provideInsertOn_exceptionOnIncomplete
336 * @covers Revision::insertOn
338 public function testInsertOn_exceptionOnIncomplete( $array, $expException, $expMessage ) {
339 // If an ExternalStore is set don't use it.
340 $this->setMwGlobals( 'wgDefaultExternalStore', false );
341 $this->setExpectedException( $expException, $expMessage );
343 $title = Title
::newFromText( 'Nonexistant-' . __METHOD__
);
344 $rev = new Revision( $array, 0, $title );
346 $rev->insertOn( wfGetDB( DB_MASTER
) );
350 * @covers Revision::newFromTitle
352 public function testNewFromTitle_withoutId() {
353 $latestRevId = $this->testPage
->getLatest();
355 $rev = Revision
::newFromTitle( $this->testPage
->getTitle() );
357 $this->assertTrue( $this->testPage
->getTitle()->equals( $rev->getTitle() ) );
358 $this->assertEquals( $latestRevId, $rev->getId() );
362 * @covers Revision::newFromTitle
364 public function testNewFromTitle_withId() {
365 $latestRevId = $this->testPage
->getLatest();
367 $rev = Revision
::newFromTitle( $this->testPage
->getTitle(), $latestRevId );
369 $this->assertTrue( $this->testPage
->getTitle()->equals( $rev->getTitle() ) );
370 $this->assertEquals( $latestRevId, $rev->getId() );
374 * @covers Revision::newFromTitle
376 public function testNewFromTitle_withBadId() {
377 $latestRevId = $this->testPage
->getLatest();
379 $rev = Revision
::newFromTitle( $this->testPage
->getTitle(), $latestRevId +
1 );
381 $this->assertNull( $rev );
385 * @covers Revision::newFromRow
387 public function testNewFromRow() {
388 $orig = $this->makeRevisionWithProps();
390 $dbr = wfGetDB( DB_REPLICA
);
391 $revQuery = Revision
::getQueryInfo();
392 $res = $dbr->select( $revQuery['tables'], $revQuery['fields'], [ 'rev_id' => $orig->getId() ],
393 __METHOD__
, [], $revQuery['joins'] );
394 $this->assertTrue( is_object( $res ), 'query failed' );
396 $row = $res->fetchObject();
399 $rev = Revision
::newFromRow( $row );
401 $this->assertRevEquals( $orig, $rev );
404 public function provideNewFromArchiveRow() {
412 return $f +
[ 'ar_namespace', 'ar_title' ];
417 unset( $f['ar_text_id'] );
423 unset( $f['ar_page_id'] );
429 unset( $f['ar_parent_id'] );
435 unset( $f['ar_rev_id'] );
441 unset( $f['ar_sha1'] );
448 * @dataProvider provideNewFromArchiveRow
449 * @covers Revision::newFromArchiveRow
451 public function testNewFromArchiveRow( $selectModifier ) {
452 $services = MediaWikiServices
::getInstance();
454 $store = new RevisionStore(
455 $services->getDBLoadBalancer(),
456 $services->getService( '_SqlBlobStore' ),
457 $services->getMainWANObjectCache(),
458 $services->getCommentStore(),
459 $services->getContentModelStore(),
460 $services->getSlotRoleStore(),
461 $services->getSlotRoleRegistry(),
462 $this->getMcrMigrationStage(),
463 $services->getActorMigration()
466 $store->setContentHandlerUseDB( $this->getContentHandlerUseDB() );
467 $this->setService( 'RevisionStore', $store );
469 $page = $this->createPage(
470 'RevisionStorageTest_testNewFromArchiveRow',
472 CONTENT_MODEL_WIKITEXT
474 $orig = $page->getRevision();
475 $page->doDeleteArticle( 'test Revision::newFromArchiveRow' );
477 $dbr = wfGetDB( DB_REPLICA
);
478 $arQuery = Revision
::getArchiveQueryInfo();
479 $arQuery['fields'] = $selectModifier( $arQuery['fields'] );
481 $arQuery['tables'], $arQuery['fields'], [ 'ar_rev_id' => $orig->getId() ],
482 __METHOD__
, [], $arQuery['joins']
484 $this->assertTrue( is_object( $res ), 'query failed' );
486 $row = $res->fetchObject();
489 // MCR migration note: $row is now required to contain ar_title and ar_namespace.
490 // Alternatively, a Title object can be passed to RevisionStore::newRevisionFromArchiveRow
491 $rev = Revision
::newFromArchiveRow( $row );
493 $this->assertRevEquals( $orig, $rev );
497 * @covers Revision::newFromArchiveRow
499 public function testNewFromArchiveRowOverrides() {
500 $page = $this->createPage(
501 'RevisionStorageTest_testNewFromArchiveRow',
503 CONTENT_MODEL_WIKITEXT
505 $orig = $page->getRevision();
506 $page->doDeleteArticle( 'test Revision::newFromArchiveRow' );
508 $dbr = wfGetDB( DB_REPLICA
);
509 $arQuery = Revision
::getArchiveQueryInfo();
511 $arQuery['tables'], $arQuery['fields'], [ 'ar_rev_id' => $orig->getId() ],
512 __METHOD__
, [], $arQuery['joins']
514 $this->assertTrue( is_object( $res ), 'query failed' );
516 $row = $res->fetchObject();
519 $rev = Revision
::newFromArchiveRow( $row, [ 'comment_text' => 'SOMEOVERRIDE' ] );
521 $this->assertNotEquals( $orig->getComment(), $rev->getComment() );
522 $this->assertEquals( 'SOMEOVERRIDE', $rev->getComment() );
526 * @covers Revision::newFromId
528 public function testNewFromId() {
529 $orig = $this->testPage
->getRevision();
530 $rev = Revision
::newFromId( $orig->getId() );
531 $this->assertRevEquals( $orig, $rev );
535 * @covers Revision::newFromPageId
537 public function testNewFromPageId() {
538 $rev = Revision
::newFromPageId( $this->testPage
->getId() );
539 $this->assertRevEquals(
540 $this->testPage
->getRevision(),
546 * @covers Revision::newFromPageId
548 public function testNewFromPageIdWithLatestId() {
549 $rev = Revision
::newFromPageId(
550 $this->testPage
->getId(),
551 $this->testPage
->getLatest()
553 $this->assertRevEquals(
554 $this->testPage
->getRevision(),
560 * @covers Revision::newFromPageId
562 public function testNewFromPageIdWithNotLatestId() {
563 $content = new WikitextContent( __METHOD__
);
564 $this->testPage
->doEditContent( $content, __METHOD__
);
565 $rev = Revision
::newFromPageId(
566 $this->testPage
->getId(),
567 $this->testPage
->getRevision()->getPrevious()->getId()
569 $this->assertRevEquals(
570 $this->testPage
->getRevision()->getPrevious(),
576 * @covers Revision::fetchRevision
578 public function testFetchRevision() {
579 // Hidden process cache assertion below
580 $this->testPage
->getRevision()->getId();
582 $this->testPage
->doEditContent( new WikitextContent( __METHOD__
), __METHOD__
);
583 $id = $this->testPage
->getRevision()->getId();
585 $this->hideDeprecated( 'Revision::fetchRevision' );
586 $res = Revision
::fetchRevision( $this->testPage
->getTitle() );
588 # note: order is unspecified
590 while ( ( $row = $res->fetchObject() ) ) {
591 $rows[$row->rev_id
] = $row;
594 $this->assertEmpty( $rows, 'expected empty set' );
598 * @covers Revision::getPage
600 public function testGetPage() {
601 $page = $this->testPage
;
603 $orig = $this->makeRevisionWithProps( [ 'page' => $page->getId() ] );
604 $rev = Revision
::newFromId( $orig->getId() );
606 $this->assertEquals( $page->getId(), $rev->getPage() );
610 * @covers Revision::isCurrent
612 public function testIsCurrent() {
613 $rev1 = $this->testPage
->getRevision();
615 # @todo find out if this should be true
616 # $this->assertTrue( $rev1->isCurrent() );
618 $rev1x = Revision
::newFromId( $rev1->getId() );
619 $this->assertTrue( $rev1x->isCurrent() );
621 $this->testPage
->doEditContent( new WikitextContent( __METHOD__
), __METHOD__
);
622 $rev2 = $this->testPage
->getRevision();
624 # @todo find out if this should be true
625 # $this->assertTrue( $rev2->isCurrent() );
627 $rev1x = Revision
::newFromId( $rev1->getId() );
628 $this->assertFalse( $rev1x->isCurrent() );
630 $rev2x = Revision
::newFromId( $rev2->getId() );
631 $this->assertTrue( $rev2x->isCurrent() );
635 * @covers Revision::getPrevious
637 public function testGetPrevious() {
638 $oldestRevision = $this->testPage
->getOldestRevision();
639 $latestRevision = $this->testPage
->getLatest();
641 $this->assertNull( $oldestRevision->getPrevious() );
643 $this->testPage
->doEditContent( new WikitextContent( __METHOD__
), __METHOD__
);
644 $newRevision = $this->testPage
->getRevision();
646 $this->assertNotNull( $newRevision->getPrevious() );
647 $this->assertEquals( $latestRevision, $newRevision->getPrevious()->getId() );
651 * @covers Revision::getNext
653 public function testGetNext() {
654 $rev1 = $this->testPage
->getRevision();
656 $this->assertNull( $rev1->getNext() );
658 $this->testPage
->doEditContent( new WikitextContent( __METHOD__
), __METHOD__
);
659 $rev2 = $this->testPage
->getRevision();
661 $this->assertNotNull( $rev1->getNext() );
662 $this->assertEquals( $rev2->getId(), $rev1->getNext()->getId() );
666 * @covers Revision::newNullRevision
668 public function testNewNullRevision() {
669 $this->testPage
->doEditContent( new WikitextContent( __METHOD__
), __METHOD__
);
670 $orig = $this->testPage
->getRevision();
672 $dbw = wfGetDB( DB_MASTER
);
673 $rev = Revision
::newNullRevision( $dbw, $this->testPage
->getId(), 'a null revision', false );
675 $this->assertNotEquals( $orig->getId(), $rev->getId(),
676 'new null revision should have a different id from the original revision' );
677 $this->assertEquals( $orig->getTextId(), $rev->getTextId(),
678 'new null revision should have the same text id as the original revision' );
679 $this->assertEquals( $orig->getSha1(), $rev->getSha1(),
680 'new null revision should have the same SHA1 as the original revision' );
681 $this->assertTrue( $orig->getRevisionRecord()->hasSameContent( $rev->getRevisionRecord() ),
682 'new null revision should have the same content as the original revision' );
683 $this->assertEquals( __METHOD__
, $rev->getContent()->getNativeData() );
687 * @covers Revision::newNullRevision
689 public function testNewNullRevision_badPage() {
690 $dbw = wfGetDB( DB_MASTER
);
691 $rev = Revision
::newNullRevision( $dbw, -1, 'a null revision', false );
693 $this->assertNull( $rev );
697 * @covers Revision::insertOn
699 public function testInsertOn() {
700 $ip = '2600:387:ed7:947e:8c16:a1ad:dd34:1dd7';
702 $orig = $this->makeRevisionWithProps( [
706 // Make sure the revision was copied to ip_changes
707 $dbr = wfGetDB( DB_REPLICA
);
708 $res = $dbr->select( 'ip_changes', '*', [ 'ipc_rev_id' => $orig->getId() ] );
709 $row = $res->fetchObject();
711 $this->assertEquals( IP
::toHex( $ip ), $row->ipc_hex
);
713 $orig->getTimestamp(),
714 wfTimestamp( TS_MW
, $row->ipc_rev_timestamp
)
718 public static function provideUserWasLastToEdit() {
719 yield
'actually the last edit' => [ 3, true ];
720 yield
'not the current edit, but still by this user' => [ 2, true ];
721 yield
'edit by another user' => [ 1, false ];
722 yield
'first edit, by this user, but another user edited in the mean time' => [ 0, false ];
726 * @covers Revision::userWasLastToEdit
727 * @dataProvider provideUserWasLastToEdit
729 public function testUserWasLastToEdit( $sinceIdx, $expectedLast ) {
730 $userA = User
::newFromName( "RevisionStorageTest_userA" );
731 $userB = User
::newFromName( "RevisionStorageTest_userB" );
733 if ( $userA->getId() === 0 ) {
734 $userA = User
::createNew( $userA->getName() );
737 if ( $userB->getId() === 0 ) {
738 $userB = User
::createNew( $userB->getName() );
741 $ns = $this->getDefaultWikitextNS();
743 $dbw = wfGetDB( DB_MASTER
);
746 // create revisions -----------------------------
747 $page = WikiPage
::factory( Title
::newFromText(
748 'RevisionStorageTest_testUserWasLastToEdit', $ns ) );
749 $page->insertOn( $dbw );
751 $revisions[0] = new Revision( [
752 'page' => $page->getId(),
753 // we need the title to determine the page's default content model
754 'title' => $page->getTitle(),
755 'timestamp' => '20120101000000',
756 'user' => $userA->getId(),
758 'content_model' => CONTENT_MODEL_WIKITEXT
,
759 'comment' => 'edit zero'
761 $revisions[0]->insertOn( $dbw );
763 $revisions[1] = new Revision( [
764 'page' => $page->getId(),
765 // still need the title, because $page->getId() is 0 (there's no entry in the page table)
766 'title' => $page->getTitle(),
767 'timestamp' => '20120101000100',
768 'user' => $userA->getId(),
770 'content_model' => CONTENT_MODEL_WIKITEXT
,
771 'comment' => 'edit one'
773 $revisions[1]->insertOn( $dbw );
775 $revisions[2] = new Revision( [
776 'page' => $page->getId(),
777 'title' => $page->getTitle(),
778 'timestamp' => '20120101000200',
779 'user' => $userB->getId(),
781 'content_model' => CONTENT_MODEL_WIKITEXT
,
782 'comment' => 'edit two'
784 $revisions[2]->insertOn( $dbw );
786 $revisions[3] = new Revision( [
787 'page' => $page->getId(),
788 'title' => $page->getTitle(),
789 'timestamp' => '20120101000300',
790 'user' => $userA->getId(),
792 'content_model' => CONTENT_MODEL_WIKITEXT
,
793 'comment' => 'edit three'
795 $revisions[3]->insertOn( $dbw );
797 $revisions[4] = new Revision( [
798 'page' => $page->getId(),
799 'title' => $page->getTitle(),
800 'timestamp' => '20120101000200',
801 'user' => $userA->getId(),
803 'content_model' => CONTENT_MODEL_WIKITEXT
,
804 'comment' => 'edit four'
806 $revisions[4]->insertOn( $dbw );
808 // test it ---------------------------------
809 $since = $revisions[$sinceIdx]->getTimestamp();
811 $revQuery = Revision
::getQueryInfo();
812 $allRows = iterator_to_array( $dbw->select(
814 [ 'rev_id', 'rev_timestamp', 'rev_user' => $revQuery['fields']['rev_user'] ],
816 'rev_page' => $page->getId(),
817 //'rev_timestamp > ' . $dbw->addQuotes( $dbw->timestamp( $since ) )
820 [ 'ORDER BY' => 'rev_timestamp ASC', 'LIMIT' => 50 ],
824 $wasLast = Revision
::userWasLastToEdit( $dbw, $page->getId(), $userA->getId(), $since );
826 $this->assertEquals( $expectedLast, $wasLast );
830 * @param string $text
831 * @param string $title
832 * @param string $model
833 * @param string $format
837 private function newTestRevision( $text, $title = "Test",
838 $model = CONTENT_MODEL_WIKITEXT
, $format = null
840 if ( is_string( $title ) ) {
841 $title = Title
::newFromText( $title );
844 $content = ContentHandler
::makeContent( $text, $title, $model, $format );
852 'content' => $content,
853 'length' => $content->getSize(),
854 'comment' => "testing",
855 'minor_edit' => false,
857 'content_format' => $format,
864 public function provideGetContentModel() {
865 // NOTE: we expect the help namespace to always contain wikitext
867 [ 'hello world', 'Help:Hello', null, null, CONTENT_MODEL_WIKITEXT
],
868 [ 'hello world', 'User:hello/there.css', null, null, CONTENT_MODEL_CSS
],
869 [ serialize( 'hello world' ), 'Dummy:Hello', null, null, DummyContentForTesting
::MODEL_ID
],
874 * @dataProvider provideGetContentModel
875 * @covers Revision::getContentModel
877 public function testGetContentModel( $text, $title, $model, $format, $expectedModel ) {
878 $rev = $this->newTestRevision( $text, $title, $model, $format );
880 $this->assertEquals( $expectedModel, $rev->getContentModel() );
883 public function provideGetContentFormat() {
884 // NOTE: we expect the help namespace to always contain wikitext
886 [ 'hello world', 'Help:Hello', null, null, CONTENT_FORMAT_WIKITEXT
],
887 [ 'hello world', 'Help:Hello', CONTENT_MODEL_CSS
, null, CONTENT_FORMAT_CSS
],
888 [ 'hello world', 'User:hello/there.css', null, null, CONTENT_FORMAT_CSS
],
889 [ serialize( 'hello world' ), 'Dummy:Hello', null, null, DummyContentForTesting
::MODEL_ID
],
894 * @dataProvider provideGetContentFormat
895 * @covers Revision::getContentFormat
897 public function testGetContentFormat( $text, $title, $model, $format, $expectedFormat ) {
898 $rev = $this->newTestRevision( $text, $title, $model, $format );
900 $this->assertEquals( $expectedFormat, $rev->getContentFormat() );
903 public function provideGetContentHandler() {
904 // NOTE: we expect the help namespace to always contain wikitext
906 [ 'hello world', 'Help:Hello', null, null, WikitextContentHandler
::class ],
907 [ 'hello world', 'User:hello/there.css', null, null, CssContentHandler
::class ],
908 [ serialize( 'hello world' ), 'Dummy:Hello', null, null, DummyContentHandlerForTesting
::class ],
913 * @dataProvider provideGetContentHandler
914 * @covers Revision::getContentHandler
916 public function testGetContentHandler( $text, $title, $model, $format, $expectedClass ) {
917 $rev = $this->newTestRevision( $text, $title, $model, $format );
919 $this->assertEquals( $expectedClass, get_class( $rev->getContentHandler() ) );
922 public function provideGetContent() {
923 // NOTE: we expect the help namespace to always contain wikitext
925 [ 'hello world', 'Help:Hello', null, null, Revision
::FOR_PUBLIC
, 'hello world' ],
927 serialize( 'hello world' ),
929 DummyContentForTesting
::MODEL_ID
,
931 Revision
::FOR_PUBLIC
,
932 serialize( 'hello world' )
935 serialize( 'hello world' ),
939 Revision
::FOR_PUBLIC
,
940 serialize( 'hello world' )
946 * @dataProvider provideGetContent
947 * @covers Revision::getContent
949 public function testGetContent( $text, $title, $model, $format,
950 $audience, $expectedSerialization
952 $rev = $this->newTestRevision( $text, $title, $model, $format );
953 $content = $rev->getContent( $audience );
956 $expectedSerialization,
957 is_null( $content ) ?
null : $content->serialize( $format )
962 * @covers Revision::getContent
964 public function testGetContent_failure() {
965 $rev = new Revision( [
966 'page' => $this->testPage
->getId(),
967 'content_model' => $this->testPage
->getContentModel(),
968 'id' => 123456789, // not in the test DB
971 Wikimedia\
suppressWarnings(); // bad text_id will trigger a warning.
973 $this->assertNull( $rev->getContent(),
974 "getContent() should return null if the revision's text blob could not be loaded." );
976 // NOTE: check this twice, once for lazy initialization, and once with the cached value.
977 $this->assertNull( $rev->getContent(),
978 "getContent() should return null if the revision's text blob could not be loaded." );
980 Wikimedia\restoreWarnings
();
983 public function provideGetSize() {
985 [ "hello world.", CONTENT_MODEL_WIKITEXT
, 12 ],
986 [ serialize( "hello world." ), DummyContentForTesting
::MODEL_ID
, 12 ],
991 * @covers Revision::getSize
992 * @dataProvider provideGetSize
994 public function testGetSize( $text, $model, $expected_size ) {
995 $rev = $this->newTestRevision( $text, 'RevisionTest_testGetSize', $model );
996 $this->assertEquals( $expected_size, $rev->getSize() );
999 public function provideGetSha1() {
1001 [ "hello world.", CONTENT_MODEL_WIKITEXT
, Revision
::base36Sha1( "hello world." ) ],
1003 serialize( "hello world." ),
1004 DummyContentForTesting
::MODEL_ID
,
1005 Revision
::base36Sha1( serialize( "hello world." ) )
1011 * @covers Revision::getSha1
1012 * @dataProvider provideGetSha1
1014 public function testGetSha1( $text, $model, $expected_hash ) {
1015 $rev = $this->newTestRevision( $text, 'RevisionTest_testGetSha1', $model );
1016 $this->assertEquals( $expected_hash, $rev->getSha1() );
1020 * Tests whether $rev->getContent() returns a clone when needed.
1022 * @covers Revision::getContent
1024 public function testGetContentClone() {
1025 $content = new RevisionTestModifyableContent( "foo" );
1027 $rev = new Revision(
1031 'title' => Title
::newFromText( "testGetContentClone_dummy" ),
1033 'content' => $content,
1034 'length' => $content->getSize(),
1035 'comment' => "testing",
1036 'minor_edit' => false,
1040 /** @var RevisionTestModifyableContent $content */
1041 $content = $rev->getContent( Revision
::RAW
);
1042 $content->setText( "bar" );
1044 /** @var RevisionTestModifyableContent $content2 */
1045 $content2 = $rev->getContent( Revision
::RAW
);
1046 // content is mutable, expect clone
1047 $this->assertNotSame( $content, $content2, "expected a clone" );
1048 // clone should contain the original text
1049 $this->assertEquals( "foo", $content2->getText() );
1051 $content2->setText( "bla bla" );
1052 // clones should be independent
1053 $this->assertEquals( "bar", $content->getText() );
1057 * Tests whether $rev->getContent() returns the same object repeatedly if appropriate.
1058 * @covers Revision::getContent
1060 public function testGetContentUncloned() {
1061 $rev = $this->newTestRevision( "hello", "testGetContentUncloned_dummy", CONTENT_MODEL_WIKITEXT
);
1062 $content = $rev->getContent( Revision
::RAW
);
1063 $content2 = $rev->getContent( Revision
::RAW
);
1065 // for immutable content like wikitext, this should be the same object
1066 $this->assertSame( $content, $content2 );
1070 * @covers Revision::loadFromId
1072 public function testLoadFromId() {
1073 $rev = $this->testPage
->getRevision();
1074 $this->hideDeprecated( 'Revision::loadFromId' );
1075 $this->assertRevEquals(
1077 Revision
::loadFromId( wfGetDB( DB_MASTER
), $rev->getId() )
1082 * @covers Revision::loadFromPageId
1084 public function testLoadFromPageId() {
1085 $this->assertRevEquals(
1086 $this->testPage
->getRevision(),
1087 Revision
::loadFromPageId( wfGetDB( DB_MASTER
), $this->testPage
->getId() )
1092 * @covers Revision::loadFromPageId
1094 public function testLoadFromPageIdWithLatestRevId() {
1095 $this->assertRevEquals(
1096 $this->testPage
->getRevision(),
1097 Revision
::loadFromPageId(
1098 wfGetDB( DB_MASTER
),
1099 $this->testPage
->getId(),
1100 $this->testPage
->getLatest()
1106 * @covers Revision::loadFromPageId
1108 public function testLoadFromPageIdWithNotLatestRevId() {
1109 $this->testPage
->doEditContent( new WikitextContent( __METHOD__
), __METHOD__
);
1110 $this->assertRevEquals(
1111 $this->testPage
->getRevision()->getPrevious(),
1112 Revision
::loadFromPageId(
1113 wfGetDB( DB_MASTER
),
1114 $this->testPage
->getId(),
1115 $this->testPage
->getRevision()->getPrevious()->getId()
1121 * @covers Revision::loadFromTitle
1123 public function testLoadFromTitle() {
1124 $this->assertRevEquals(
1125 $this->testPage
->getRevision(),
1126 Revision
::loadFromTitle( wfGetDB( DB_MASTER
), $this->testPage
->getTitle() )
1131 * @covers Revision::loadFromTitle
1133 public function testLoadFromTitleWithLatestRevId() {
1134 $this->assertRevEquals(
1135 $this->testPage
->getRevision(),
1136 Revision
::loadFromTitle(
1137 wfGetDB( DB_MASTER
),
1138 $this->testPage
->getTitle(),
1139 $this->testPage
->getLatest()
1145 * @covers Revision::loadFromTitle
1147 public function testLoadFromTitleWithNotLatestRevId() {
1148 $this->testPage
->doEditContent( new WikitextContent( __METHOD__
), __METHOD__
);
1149 $this->assertRevEquals(
1150 $this->testPage
->getRevision()->getPrevious(),
1151 Revision
::loadFromTitle(
1152 wfGetDB( DB_MASTER
),
1153 $this->testPage
->getTitle(),
1154 $this->testPage
->getRevision()->getPrevious()->getId()
1160 * @covers Revision::loadFromTimestamp()
1162 public function testLoadFromTimestamp() {
1163 $this->assertRevEquals(
1164 $this->testPage
->getRevision(),
1165 Revision
::loadFromTimestamp(
1166 wfGetDB( DB_MASTER
),
1167 $this->testPage
->getTitle(),
1168 $this->testPage
->getRevision()->getTimestamp()
1174 * @covers Revision::getParentLengths
1176 public function testGetParentLengths_noRevIds() {
1179 Revision
::getParentLengths(
1180 wfGetDB( DB_MASTER
),
1187 * @covers Revision::getParentLengths
1189 public function testGetParentLengths_oneRevId() {
1190 $text = '831jr091jr0921kr21kr0921kjr0921j09rj1';
1191 $textLength = strlen( $text );
1193 $this->testPage
->doEditContent( new WikitextContent( $text ), __METHOD__
);
1194 $rev[1] = $this->testPage
->getLatest();
1197 [ $rev[1] => $textLength ],
1198 Revision
::getParentLengths(
1199 wfGetDB( DB_MASTER
),
1206 * @covers Revision::getParentLengths
1208 public function testGetParentLengths_multipleRevIds() {
1209 $textOne = '831jr091jr0921kr21kr0921kjr0921j09rj1';
1210 $textOneLength = strlen( $textOne );
1211 $textTwo = '831jr091jr092121j09rj1';
1212 $textTwoLength = strlen( $textTwo );
1214 $this->testPage
->doEditContent( new WikitextContent( $textOne ), __METHOD__
);
1215 $rev[1] = $this->testPage
->getLatest();
1216 $this->testPage
->doEditContent( new WikitextContent( $textTwo ), __METHOD__
);
1217 $rev[2] = $this->testPage
->getLatest();
1220 [ $rev[1] => $textOneLength, $rev[2] => $textTwoLength ],
1221 Revision
::getParentLengths(
1222 wfGetDB( DB_MASTER
),
1223 [ $rev[1], $rev[2] ]
1229 * @covers Revision::getTitle
1231 public function testGetTitle_fromExistingRevision() {
1233 $this->testPage
->getTitle()->equals(
1234 $this->testPage
->getRevision()->getTitle()
1240 * @covers Revision::getTitle
1242 public function testGetTitle_fromRevisionWhichWillLoadTheTitle() {
1243 $rev = new Revision( [ 'id' => $this->testPage
->getLatest() ] );
1245 $this->testPage
->getTitle()->equals(
1252 * @covers Revision::isMinor
1254 public function testIsMinor_true() {
1255 // Use a sysop to ensure we can mark edits as minor
1256 $sysop = $this->getTestSysop()->getUser();
1258 $this->testPage
->doEditContent(
1259 new WikitextContent( __METHOD__
),
1265 $rev = $this->testPage
->getRevision();
1267 $this->assertSame( true, $rev->isMinor() );
1271 * @covers Revision::isMinor
1273 public function testIsMinor_false() {
1274 $this->testPage
->doEditContent(
1275 new WikitextContent( __METHOD__
),
1279 $rev = $this->testPage
->getRevision();
1281 $this->assertSame( false, $rev->isMinor() );
1285 * @covers Revision::getTimestamp
1287 public function testGetTimestamp() {
1288 $testTimestamp = wfTimestampNow();
1290 $this->testPage
->doEditContent(
1291 new WikitextContent( __METHOD__
),
1294 $rev = $this->testPage
->getRevision();
1296 $this->assertInternalType( 'string', $rev->getTimestamp() );
1297 $this->assertTrue( strlen( $rev->getTimestamp() ) == strlen( 'YYYYMMDDHHMMSS' ) );
1298 $this->assertContains( substr( $testTimestamp, 0, 10 ), $rev->getTimestamp() );
1302 * @covers Revision::getUser
1303 * @covers Revision::getUserText
1305 public function testGetUserAndText() {
1306 $sysop = $this->getTestSysop()->getUser();
1308 $this->testPage
->doEditContent(
1309 new WikitextContent( __METHOD__
),
1315 $rev = $this->testPage
->getRevision();
1317 $this->assertSame( $sysop->getId(), $rev->getUser() );
1318 $this->assertSame( $sysop->getName(), $rev->getUserText() );
1322 * @covers Revision::isDeleted
1324 public function testIsDeleted_nothingDeleted() {
1325 $rev = $this->testPage
->getRevision();
1327 $this->assertSame( false, $rev->isDeleted( Revision
::DELETED_TEXT
) );
1328 $this->assertSame( false, $rev->isDeleted( Revision
::DELETED_COMMENT
) );
1329 $this->assertSame( false, $rev->isDeleted( Revision
::DELETED_RESTRICTED
) );
1330 $this->assertSame( false, $rev->isDeleted( Revision
::DELETED_USER
) );
1334 * @covers Revision::getVisibility
1336 public function testGetVisibility_nothingDeleted() {
1337 $rev = $this->testPage
->getRevision();
1339 $this->assertSame( 0, $rev->getVisibility() );
1343 * @covers Revision::getComment
1345 public function testGetComment_notDeleted() {
1346 $expectedSummary = 'goatlicious summary';
1348 $this->testPage
->doEditContent(
1349 new WikitextContent( __METHOD__
),
1352 $rev = $this->testPage
->getRevision();
1354 $this->assertSame( $expectedSummary, $rev->getComment() );
1358 * @covers Revision::isUnpatrolled
1360 public function testIsUnpatrolled_returnsRecentChangesId() {
1361 $this->testPage
->doEditContent( new WikitextContent( __METHOD__
), __METHOD__
);
1362 $rev = $this->testPage
->getRevision();
1364 $this->assertGreaterThan( 0, $rev->isUnpatrolled() );
1365 $this->assertSame( $rev->getRecentChange()->getAttribute( 'rc_id' ), $rev->isUnpatrolled() );
1369 * @covers Revision::isUnpatrolled
1371 public function testIsUnpatrolled_returnsZeroIfPatrolled() {
1372 // This assumes that sysops are auto patrolled
1373 $sysop = $this->getTestSysop()->getUser();
1374 $this->testPage
->doEditContent(
1375 new WikitextContent( __METHOD__
),
1381 $rev = $this->testPage
->getRevision();
1383 $this->assertSame( 0, $rev->isUnpatrolled() );
1387 * This is a simple blanket test for all simple content getters and is methods to provide some
1388 * coverage before the split of Revision into multiple classes for MCR work.
1389 * @covers Revision::getContent
1390 * @covers Revision::getSerializedData
1391 * @covers Revision::getContentModel
1392 * @covers Revision::getContentFormat
1393 * @covers Revision::getContentHandler
1395 public function testSimpleContentGetters() {
1396 $expectedText = 'testSimpleContentGetters in Revision. Goats love MCR...';
1397 $expectedSummary = 'goatlicious testSimpleContentGetters summary';
1399 $this->testPage
->doEditContent(
1400 new WikitextContent( $expectedText ),
1403 $rev = $this->testPage
->getRevision();
1405 $this->assertSame( $expectedText, $rev->getContent()->getNativeData() );
1406 $this->assertSame( $expectedText, $rev->getSerializedData() );
1407 $this->assertSame( $this->testPage
->getContentModel(), $rev->getContentModel() );
1408 $this->assertSame( $this->testPage
->getContent()->getDefaultFormat(), $rev->getContentFormat() );
1409 $this->assertSame( $this->testPage
->getContentHandler(), $rev->getContentHandler() );
1413 * @covers Revision::newKnownCurrent
1415 public function testNewKnownCurrent() {
1416 // Setup the services
1417 $this->overrideMwServices();
1418 $cache = new WANObjectCache( [ 'cache' => new HashBagOStuff() ] );
1419 $this->setService( 'MainWANObjectCache', $cache );
1420 $db = wfGetDB( DB_MASTER
);
1422 // Get a fresh revision to use during testing
1423 $this->testPage
->doEditContent( new WikitextContent( __METHOD__
), __METHOD__
);
1424 $rev = $this->testPage
->getRevision();
1426 // Clear any previous cache for the revision during creation
1427 $key = $cache->makeGlobalKey(
1428 RevisionStore
::ROW_CACHE_KEY
,
1433 $cache->delete( $key, WANObjectCache
::HOLDOFF_NONE
);
1434 $this->assertFalse( $cache->get( $key ) );
1436 // Get the new revision and make sure it is in the cache and correct
1437 $newRev = Revision
::newKnownCurrent( $db, $rev->getPage(), $rev->getId() );
1438 $this->assertRevEquals( $rev, $newRev );
1440 $cachedRow = $cache->get( $key );
1441 $this->assertNotFalse( $cachedRow );
1442 $this->assertEquals( $rev->getId(), $cachedRow->rev_id
);
1445 public function testNewKnownCurrent_withPageId() {
1446 $db = wfGetDB( DB_MASTER
);
1448 $this->testPage
->doEditContent( new WikitextContent( __METHOD__
), __METHOD__
);
1449 $rev = $this->testPage
->getRevision();
1451 $pageId = $this->testPage
->getId();
1453 $newRev = Revision
::newKnownCurrent( $db, $pageId, $rev->getId() );
1454 $this->assertRevEquals( $rev, $newRev );
1457 public function testNewKnownCurrent_returnsFalseWhenTitleDoesntExist() {
1458 $db = wfGetDB( DB_MASTER
);
1460 $this->assertFalse( Revision
::newKnownCurrent( $db, 0 ) );
1463 public function provideUserCanBitfield() {
1464 yield
[ 0, 0, [], null, true ];
1465 // Bitfields match, user has no permissions
1466 yield
[ Revision
::DELETED_TEXT
, Revision
::DELETED_TEXT
, [], null, false ];
1467 yield
[ Revision
::DELETED_COMMENT
, Revision
::DELETED_COMMENT
, [], null, false ];
1468 yield
[ Revision
::DELETED_USER
, Revision
::DELETED_USER
, [], null, false ];
1469 yield
[ Revision
::DELETED_RESTRICTED
, Revision
::DELETED_RESTRICTED
, [], null, false ];
1470 // Bitfields match, user (admin) does have permissions
1471 yield
[ Revision
::DELETED_TEXT
, Revision
::DELETED_TEXT
, [ 'sysop' ], null, true ];
1472 yield
[ Revision
::DELETED_COMMENT
, Revision
::DELETED_COMMENT
, [ 'sysop' ], null, true ];
1473 yield
[ Revision
::DELETED_USER
, Revision
::DELETED_USER
, [ 'sysop' ], null, true ];
1474 // Bitfields match, user (admin) does not have permissions
1475 yield
[ Revision
::DELETED_RESTRICTED
, Revision
::DELETED_RESTRICTED
, [ 'sysop' ], null, false ];
1476 // Bitfields match, user (oversight) does have permissions
1477 yield
[ Revision
::DELETED_RESTRICTED
, Revision
::DELETED_RESTRICTED
, [ 'oversight' ], null, true ];
1478 // Check permissions using the title
1480 Revision
::DELETED_TEXT
,
1481 Revision
::DELETED_TEXT
,
1487 Revision
::DELETED_TEXT
,
1488 Revision
::DELETED_TEXT
,
1496 * @dataProvider provideUserCanBitfield
1497 * @covers Revision::userCanBitfield
1499 public function testUserCanBitfield( $bitField, $field, $userGroups, $title, $expected ) {
1500 $title = Title
::newFromText( $title );
1502 $this->setMwGlobals(
1503 'wgGroupPermissions',
1506 'deletedtext' => true,
1507 'deletedhistory' => true,
1510 'viewsuppressed' => true,
1511 'suppressrevision' => true,
1515 $user = $this->getTestUser( $userGroups )->getUser();
1519 Revision
::userCanBitfield( $bitField, $field, $user, $title )
1522 // Fallback to $wgUser
1523 $this->setMwGlobals(
1529 Revision
::userCanBitfield( $bitField, $field, null, $title )
1533 public function provideUserCan() {
1534 yield
[ 0, 0, [], true ];
1535 // Bitfields match, user has no permissions
1536 yield
[ Revision
::DELETED_TEXT
, Revision
::DELETED_TEXT
, [], false ];
1537 yield
[ Revision
::DELETED_COMMENT
, Revision
::DELETED_COMMENT
, [], false ];
1538 yield
[ Revision
::DELETED_USER
, Revision
::DELETED_USER
, [], false ];
1539 yield
[ Revision
::DELETED_RESTRICTED
, Revision
::DELETED_RESTRICTED
, [], false ];
1540 // Bitfields match, user (admin) does have permissions
1541 yield
[ Revision
::DELETED_TEXT
, Revision
::DELETED_TEXT
, [ 'sysop' ], true ];
1542 yield
[ Revision
::DELETED_COMMENT
, Revision
::DELETED_COMMENT
, [ 'sysop' ], true ];
1543 yield
[ Revision
::DELETED_USER
, Revision
::DELETED_USER
, [ 'sysop' ], true ];
1544 // Bitfields match, user (admin) does not have permissions
1545 yield
[ Revision
::DELETED_RESTRICTED
, Revision
::DELETED_RESTRICTED
, [ 'sysop' ], false ];
1546 // Bitfields match, user (oversight) does have permissions
1547 yield
[ Revision
::DELETED_RESTRICTED
, Revision
::DELETED_RESTRICTED
, [ 'oversight' ], true ];
1551 * @dataProvider provideUserCan
1552 * @covers Revision::userCan
1554 public function testUserCan( $bitField, $field, $userGroups, $expected ) {
1555 $this->setMwGlobals(
1556 'wgGroupPermissions',
1559 'deletedtext' => true,
1560 'deletedhistory' => true,
1563 'viewsuppressed' => true,
1564 'suppressrevision' => true,
1568 $user = $this->getTestUser( $userGroups )->getUser();
1569 $revision = new Revision( [ 'deleted' => $bitField ], 0, $this->testPage
->getTitle() );
1573 $revision->userCan( $field, $user )
1577 public function provideGetTextId() {
1580 $slot = new SlotRecord( (object)[
1581 'slot_revision_id' => 42,
1582 'slot_content_id' => 1,
1583 'content_address' => 'tt:789',
1584 'model_name' => CONTENT_MODEL_WIKITEXT
,
1585 'role_name' => SlotRecord
::MAIN
,
1587 ], new WikitextContent( 'Test' ) );
1589 $rec = new MutableRevisionRecord( $this->testPage
->getTitle() );
1591 $rec->setSlot( $slot );
1593 yield
[ $rec, 789 ];
1597 * @dataProvider provideGetTextId
1598 * @covers Revision::getTextId()
1600 public function testGetTextId( $spec, $expected ) {
1601 $rev = new Revision( $spec, 0, $this->testPage
->getTitle() );
1602 $this->assertSame( $expected, $rev->getTextId() );
1605 public function provideGetRevisionText() {
1612 * @dataProvider provideGetRevisionText
1613 * @covers Revision::getRevisionText
1615 public function testGetRevisionText( array $queryInfoOptions, array $queryInfoExtra = [] ) {
1616 $rev = $this->testPage
->getRevisionRecord();
1618 $queryInfo = Revision
::getQueryInfo( $queryInfoOptions );
1619 $queryInfo['tables'] = array_merge( $queryInfo['tables'], $queryInfoExtra['tables'] ??
[] );
1620 $queryInfo['fields'] = array_merge( $queryInfo['fields'], $queryInfoExtra['fields'] ??
[] );
1621 $queryInfo['joins'] = array_merge( $queryInfo['joins'], $queryInfoExtra['joins'] ??
[] );
1623 $conds = [ 'rev_id' => $rev->getId() ];
1624 $row = $this->db
->selectRow(
1625 $queryInfo['tables'],
1626 $queryInfo['fields'],
1633 $expected = $rev->getContent( SlotRecord
::MAIN
)->serialize();
1635 $this->hideDeprecated( 'Revision::getRevisionText (MCR without SCHEMA_COMPAT_WRITE_OLD)' );
1636 $this->assertSame( $expected, Revision
::getRevisionText( $row ) );